<template>
  <div class="app-container">
    <el-button type="primary" @click="handleAdd">创建等级</el-button>

    <el-table :data="memberLevelList" style="width: 100%;margin-top:30px;" stripe>
      <el-table-column 
        prop="name" 
        align="center" 
        label="等级名称">
      </el-table-column>
      <el-table-column 
        align="center" 
        label="是否默认等级">
        <template slot-scope="scope">{{ scope.row.defaultStatus === 0 ? '否' : '是' }}</template>
      </el-table-column>
      <el-table-column 
        prop="growthPoint" 
        align="center" 
        label="满足成长值">
      </el-table-column>
      <el-table-column 
        align="center" 
        label="是否享有免邮">
        <template slot-scope="scope">{{ scope.row.priviledgeFreeFreight === 0 ? '否' : '是' }}</template>
      </el-table-column>
      <el-table-column 
        prop="note" 
        align="center" 
        label="备注">
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'编辑会员等级':'创建会员等级'">
      <el-form :model="memberLevel" label-width="80px" label-position="left">
        <el-form-item label="等级名称">
          <el-input v-model="memberLevel.name" placeholder="等级名称" />
        </el-form-item>
        <el-form-item label="是否默认等级">
            <el-radio v-model="memberLevel.defaultStatus" label="0">否</el-radio>
            <el-radio v-model="memberLevel.defaultStatus" label="1">是</el-radio>
        </el-form-item>
        <el-form-item label="满足成长值">
          <el-input v-model="memberLevel.growthPoint" placeholder="满足成长值" />
        </el-form-item>
        <el-form-item label="是否享有免邮">
            <el-radio v-model="memberLevel.priviledgeFreeFreight" label="0">否</el-radio>
            <el-radio v-model="memberLevel.priviledgeFreeFreight" label="1">是</el-radio>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="memberLevel.note"
            :autosize="{ minRows: 2, maxRows: 4}"
            type="textarea"
            placeholder="备注"
          />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import path from 'path'
import { deepClone } from '@/utils'
import { getRoutes, getRoles, addRole, deleteRole, updateRole } from '@/api/role'
import { getMenus, getParents, addMenu, updateMenu, delMenu } from '@/api/permission'
import { getMemberLevels, addMemberLevel, getMemberLevelDetal, updateMemberLevel, delMemberLevel } from '@/api/member'

const defaultRole = {
  key: '',
  name: '',
  description: '',
  routes: []
}

export default {
  data() {
    return {
      memberLevel: {
        id: '',
        name: '',
        defaultStatus: '0',
        growthPoint: '',
        priviledgeFreeFreight: '0',
        note: ''
      },
      memberLevelList: [],
      dialogVisible: false,
      dialogType: 'new',
      checkStrictly: false,
      defaultProps: {
        children: 'children',
        label: 'title'
      }
    }
  },
  computed: {
    routesData() {
      return this.routes
    }
  },
  created() {
    // Mock: get all routes and roles list from server
    this.getMemberLevels()
    // this.getParentList()
  },
  methods: {
    async getMemberLevels() {
      const res = await getMemberLevels()
      console.log("getMemberLevels",res)
      // this.serviceRoutes = res.data
      this.memberLevelList = res.data
    },
    handleAdd() {
      // console.log("menu",this.menu)
      // console.log("menuAdd",this.menuAdd)
      this.memberLevel = {}
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    async handleEdit(scope) {
      const editMenu = scope.row
      const res = await getMemberLevelDetal(scope.row.id)
      console.log('handleEdit',res)
      this.memberLevel = res.data
      this.memberLevel.defaultStatus = String(res.data.defaultStatus)
      this.memberLevel.priviledgeFreeFreight = String(res.data.priviledgeFreeFreight)
      this.dialogType = 'edit'
      this.dialogVisible = true
    },
    handleDelete(scope) {
      this.$confirm('确认删除会员等级：'+scope.row.name+'?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delMemberLevel(scope.row.id).then(() => {
            this.$message({
              type: 'success',
              message: '删除菜单成功'
            })
          })

          this.getMemberLevels()
        })
        .catch(() => { 
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
         })
    },

    async confirmSubmit() {
      const isEdit = this.dialogType === 'edit'

      if (isEdit) {
        await updateMemberLevel(this.memberLevel).then(() => {
          this.$message({
            type: 'success',
            message: '更新会员等级成功!'
          })
        })
      } else {
        await addMemberLevel(this.memberLevel).then(() => {
          this.$message({
              type: 'success',
              message: '创建会员等级成功!'
            })
        })
      }

      this.dialogVisible = false
      this.getMemberLevels()

    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
